package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.ResultCode;
import com.pwt.common.ResultPage;
import com.pwt.entity.*;
import com.pwt.entity.ProductStatistics;
import com.pwt.model.StatisticsPercentModel;
import com.pwt.repository.ProductStatisticsRepository;
import com.pwt.repository.StatisticsPercentRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * ProductStatistics service层
 *
 * @author auto generated
 * @date 2021-08-31 12:03:28
 */
@Service
public class ProductStatisticsService {

    private ProductStatisticsRepository productStatisticsRepository;

    private StatisticsPercentRepository statisticsPercentRepository;

    public ProductStatisticsService(ProductStatisticsRepository productStatisticsRepository, StatisticsPercentRepository statisticsPercentRepository) {
        this.productStatisticsRepository = productStatisticsRepository;
        this.statisticsPercentRepository = statisticsPercentRepository;
    }

    public List<ProductStatistics> getList(String name){
//        ,Integer offset,Integer limit
        Specification<ProductStatistics> spec = new Specification<ProductStatistics>() {
            @Override
            public Predicate toPredicate(Root<ProductStatistics> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(StringUtils.isNotBlank(name)){
                    Predicate predicate= criteriaBuilder.like(root.get("name").as(String.class),name+"%");
                    list.add(predicate);
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        };
//        Pageable pageable = PageRequest.of(offset-1,limit, Sort.Direction.DESC,"createTime");
        List<ProductStatistics> list = this.productStatisticsRepository.findAll(spec, Sort.by("createTime").descending());
        return list;
    }


    @Transactional(rollbackFor = Exception.class)
    public ProductStatistics add(ProductStatistics productStatisticsModel) throws CustomException {
        ProductStatistics byType = this.productStatisticsRepository.getByType(productStatisticsModel.getType());
        if(byType != null){
            throw new CustomException(ResultCode.DATA_EXISTED);
        }
        productStatisticsModel = this.productStatisticsRepository.save(productStatisticsModel);
        if(productStatisticsModel == null){
            throw new CustomException("添加失败！",new RuntimeException());
        }
        return productStatisticsModel;
    }



    @Transactional(rollbackFor = Exception.class)
    public ProductStatistics update(ProductStatistics productStatisticsModel) throws CustomException {
        ProductStatistics byType = this.productStatisticsRepository.getByType(productStatisticsModel.getType());
        if( byType != null && !byType.getId().equals(productStatisticsModel.getId())){
            throw new CustomException(ResultCode.DATA_EXISTED);
        }
        ProductStatistics productStatistics = this.productStatisticsRepository.getById(productStatisticsModel.getId());
        productStatistics.setType(productStatisticsModel.getType());
        productStatistics.setColor(productStatisticsModel.getColor());
        productStatistics = this.productStatisticsRepository.save(productStatistics);
        if(productStatistics == null){
            throw new CustomException("添加失败！",new RuntimeException());
        }
        return productStatistics;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String id){
        this.productStatisticsRepository.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<StatisticsPercent> PercentSet(StatisticsPercentModel statisticsPercentModel) throws CustomException {
        String productStatisticsId= statisticsPercentModel.getProductStatisticsId();
        if(productStatisticsId == null){
            throw new CustomException("统计id不能为空！");
        }
        this.statisticsPercentRepository.deleteByProductStatisticsId(productStatisticsId);
        List<StatisticsPercent> list= statisticsPercentModel.getList();
        if(list != null && list.size() > 0 ){
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i) != null){
                    list.get(i).setProductStatisticsId(productStatisticsId);
                    list.get(i).setId(null);
                }
            }
        }
        list = this.statisticsPercentRepository.saveAll(list);
        return list;
    }

    public List<StatisticsPercent> getPercentByStatistics(String id){
      return  this.statisticsPercentRepository.getByProductStatisticsId(id);
    }
}
